package com.cellulam.api.config;

import com.alibaba.druid.support.http.WebStatFilter;
import com.cellulam.api.config.filters.RequestFilter;
import com.cellulam.properties.SystemProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import javax.annotation.PostConstruct;

@Configuration
@Slf4j
public class ApplicationConfig {

    @Autowired
    private SystemProperties systemProperties;

    @Autowired
    private RequestFilter requestFilter;

    @PostConstruct
    public void init() {
    }

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE + 20)
    public FilterRegistrationBean filterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(requestFilter);
        registration.addUrlPatterns("/*");
        registration.setName("requestFilter");
        return registration;
    }
//
//    @Bean
//    @Order(Ordered.HIGHEST_PRECEDENCE)
//    public FilterRegistrationBean authRegistration() {
//        FilterRegistrationBean registration = new FilterRegistrationBean();
//        registration.setFilter(new LoginAuthFilter());
//        registration.addUrlPatterns("/*");
//        registration.setName("requestFilter");
//        return registration;
//    }

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE + 10)
    public FilterRegistrationBean webStatFilterRegistration() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

    public CommandLineRunner commandLineRunner(ApplicationContext context) {
        return args -> log.info("using active profile is {}", systemProperties.getActiveProfile());
    }
}
